// Created by WXX on 2021/12/19 17:08
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * 执行用时：152 ms, 在所有 C++ 提交中击败了58.92%的用户
 * 内存消耗：67.3 MB, 在所有 C++ 提交中击败了18.75%的用户
 */
class Solution {
public:
    int findJudge(int n, vector<vector<int>>& trust) {
        vector<int> din(n + 1), dout(n + 1);
        for (auto p : trust) dout[p[0]]++, din[p[1]]++;

        int res = -1;
        for (int i = 1; i <= n; i++) {
            if (din[i] == n - 1 && dout[i] == 0) {
                if (res != -1) return -1;
                res = i;
            }
        }
        return res;
    }
};

int main() {

    vector<vector<int>> trust = {{1, 3}, {2, 3}};
    cout << Solution().findJudge(3, trust) << endl;  // 3

    return 0;
}
